package com.cxb.mqtt;

import android.content.Context;
import android.content.Intent;
import android.os.Debug;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.amitek.aidl.mqtt.MqttServiceCallback;
import com.amitek.aidl.mqtt.bean.BindStatus;
import com.amitek.aidl.mqtt.bean.ChargeStatus;
import com.amitek.aidl.mqtt.bean.ConfigStatus;
import com.amitek.aidl.mqtt.bean.GroupStatus;
import com.amitek.aidl.mqtt.bean.MonitorRequest;
import com.amitek.aidl.mqtt.bean.OtaStatus;
import com.amitek.aidl.mqtt.bean.MonitorRequest.MediaType;
import com.amitek.aidl.mqtt.bean.NaviRequest;
import com.amitek.aidl.mqtt.bean.NaviRequest.DestInfo;
import com.amitek.aidl.mqtt.bean.SystemRequest;
import com.amitek.aidl.mqtt.bean.TrackRequest;
import com.cxb.R;
import com.cxb.entity.EventBusMessage;
import com.cxb.entity.GpsDestinationInfo;
import com.cxb.manager.NavigationManager;
import com.cxb.utils.CommonUtils;
import com.cxb.utils.SharedPreferencesUtils;
import com.cxb.utils.Tts;
import com.cxb.utils.UpdateUtils;

import de.greenrobot.event.EventBus;

public class MqttCallBack implements MqttServiceCallback {

	private NavigationManager mNavigationManager;
	private Context mContext;
	private Handler mHandler;
	private SharedPreferencesUtils spUtils;

	public MqttCallBack(Context context) {
		mContext = context;
		mHandler = new Handler();
		spUtils = new SharedPreferencesUtils(context);
	}

	@Override
	public void onBindStatus(final BindStatus status) {
		spUtils.setBoundRecord(true);

		if (spUtils.getWeixinBind() == status.bindStatus) {
			return;
		}
		spUtils.setWeixinBind(status.bindStatus);

		EventBus.getDefault().post(
				new EventBusMessage(
						EventBusMessage.EVENT_TYPE_WEIXIN_BIND_STATE_CHANGE));

		mHandler.post(new Runnable() {

			@Override
			public void run() {
				if (status.bindStatus && status.weixin != null) {
					Tts.showToast(mContext, mContext.getString(
							R.string.has_bind_weixin_tts,
							status.weixin.nickName), Toast.LENGTH_SHORT);
				} else if (status.weixin != null) {
					Tts.showToast(mContext, mContext.getString(
							R.string.has_unbind_weixin_tts,
							status.weixin.nickName), Toast.LENGTH_SHORT);
				}

			}
		});

	}

	@Override
	public void onChargeStatus(ChargeStatus arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onConfigStatus(final ConfigStatus configStatus) {
		if (configStatus.otaStatus != null) {

			mHandler.postDelayed(new Runnable() {

				@Override
				public void run() {
					com.cxb.utils.Debug.printfLog(configStatus.toString());
					com.cxb.utils.Debug.printfLog("currentversion="
							+ CommonUtils
									.getSystemProperties("cxb.software.version"));
					OtaStatus otaStatus = configStatus.otaStatus;
					if (CommonUtils.getSystemProperties("cxb.software.version")
							.equals(otaStatus.version)) {
						com.cxb.utils.Debug.printfLog("ignore ota update");
						return;
					}
					UpdateUtils.startUpdate(mContext, configStatus.otaStatus);
				}
			}, 30000);

		}

	}

	@Override
	public void onGroupStatus(GroupStatus arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onMonitorRequest(MonitorRequest monitorRequest) {

		MediaType mediaType = monitorRequest.mediaType;
		Intent intent;
		if (mediaType == MediaType.MEDIA_PHOTO) {
			// 图片
			intent = new Intent();
			intent.setClassName("com.cxb.carrecorder",
					"com.cxb.carrecorder.PreviewService");
			intent.setAction("ACTION_GET_PICTURE_SHARE");
			mContext.startService(intent);
		} else if (mediaType == MediaType.MEDIA_VIDEO) {
			// 视频
			intent = new Intent();
			intent.setClassName("com.cxb.carrecorder",
					"com.cxb.carrecorder.PreviewService");
			intent.setAction("ACTION_GET_VIDEO_SHARE");
			mContext.startService(intent);
		}

	}

	@Override
	public void onNaviRequest(NaviRequest naviRequest) {

		Log.i("test", "onNaviRequest navi=" + naviRequest);
		if (mNavigationManager == null) {
			mNavigationManager = new NavigationManager(mContext);
		}
		final DestInfo destInfo = naviRequest.destArray.get(0);

		final GpsDestinationInfo naviDestination = new GpsDestinationInfo();
		naviDestination.destName = destInfo.address;
		naviDestination.latitude = destInfo.latitude;
		naviDestination.longitude = destInfo.longitude;
		naviDestination.groupName = naviRequest.groupName;
		mHandler.postDelayed(new Runnable() {

			@Override
			public void run() {
				mNavigationManager.dealNaviDelay(naviDestination);
			}
		}, 0);

	}

	@Override
	public void onSystemRequest(SystemRequest arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTrackRequest(TrackRequest arg0) {
		// TODO Auto-generated method stub

	}

}
